<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Project_m extends FOX_Model
{
    function __construct()
    {
        parent::__construct();
    }

    public function add_project($data){
        $this->db->insert('project',$data);
        if($this->db->affected_rows() > 0){
            return $this->db->insert_id();
        }else{
            return FALSE;
        }

    }
    public function get_project_list($limit,$start,$key){
        $this->db->select('*');
        $this->db->from('project');
//        $this->db->join('auth_groups','auth_groups.id = auth_group_access.group_id','left');
        $this->db->limit($start,$limit);
        if($key){
            $this->db->like('project_name', $key);
        }
        $this->db->where('delete',0);
        $this->db->order_by('project_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function get_user_project_list($limit,$start,$key,$user_id){
        $this->db->select('*');
        $this->db->from('project');
//        $this->db->join('auth_groups','auth_groups.id = auth_group_access.group_id','left');
        $this->db->limit($start,$limit);
        if($key){
            $this->db->like('project_name', $key);
        }
        $this->db->like('project_user_id',$user_id);
        $this->db->where('delete',0);
        $this->db->order_by('project_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function count_project_find($key){
        $this->db->select('project_id');
        if($key){
            $this->db->like('project_name',$key);
        }
        $this->db->from('project');
        $this->db->where('delete',0);
        $total=$this->db->count_all_results();
        return $total;
    }
    public function count_user_project_find($key,$user_id){
        $this->db->select('project_id');
        if($key){
            $this->db->like('project_name',$key);
        }
        $this->db->like('project_user_id',$user_id);
        $this->db->from('project');
        $this->db->where('delete',0);
        $total=$this->db->count_all_results();
        return $total;
    }

    public function get_one_project($project_id){
        $this->db->select('*');
        $this->db->from('project');
        $this->db->where('project_id',$project_id);
        $query = $this->db->get();
        $res = $query->row_array();
        return $res;
    }
    public function update_project($project_id,$data){
        $this->db->where('project_id',$project_id);
        $this->db->update('project', $data);
        return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
    }
    public function get_all_project(){
        $this->db->select('*');
        $this->db->from('project');
        $this->db->where('delete',0);
        $this->db->order_by('project_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }
    }
    public function count_user_project($user_id=''){
        $this->db->select('project_id');
        if($user_id){
            $this->db->like('project_user_id',$user_id);
        }
        $this->db->from('project');
        $this->db->where('delete',0);
        $total=$this->db->count_all_results();
        return $total;
    }
    public function count_user_sucess_project($user_id=''){
        $this->db->select('project_id');
        if($user_id){
            $this->db->like('project_user_id',$user_id);
        }
        $this->db->from('project');
        $this->db->where('delete',0);
        $this->db->where('project_status',3);
        $total=$this->db->count_all_results();
        return $total;
    }
    public function count_user_ing_project($user_id=''){
        $this->db->select('project_id');
        if($user_id){
            $this->db->like('project_user_id',$user_id);
        }
        $this->db->from('project');
        $this->db->where('delete',0);
        $this->db->where('project_status!=',3);
        $total=$this->db->count_all_results();
        return $total;
    }
    public function count_project_totla_monney(){
        $this->db->select_sum('project_total_money');
        $this->db->from('project');
        $this->db->where('delete',0);
        $query = $this->db->get();
        $result = $query->result();
        return ($result[0]->project_total_money)?$result[0]->project_total_money:'0.00';

    }

}
